package com.nsgf.library.ui.page;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.bean.RepairBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;

public class MaintenanceDetailsActivity extends BaseActivity {

    private MaintenanceDetailsStates mStates;

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = new MaintenanceDetailsStates();
        mAccountRequester = new AccountRequester();
    }

    private String pdfUrl = "";


    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_maintenance_details, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mStates.type.set(getIntent().getIntExtra("type", 0));
        mStates.titleBar.set(mStates.type.get() == 0 ? "维修项目" : "养护项目");
        mStates.dataBean.set(getIntent().getParcelableExtra("bean"));

        mAccountRequester.getRepairInfo(mStates.type.get(), mStates.dataBean.get().getId());

        mAccountRequester.getRepairResult().observe(this, new Observer<ResponseStatus<RepairBean>>() {
            @Override
            public void onChanged(ResponseStatus<RepairBean> repairBeanResponseStatus) {
                if (repairBeanResponseStatus.getResponseCode() == 200) {
                    mStates.repairBean.set(repairBeanResponseStatus.getData());

                    if (1 == mStates.type.get()) {
                        mStates.repairBean.get().setBuRepairInfo(mStates.repairBean.get().getBuNurtureInfo());
                        mStates.repairBean.get().getBuRepairInfo().setTechManagerReal(mStates.repairBean.get().getBuNurturePrjState().getConstructionManager());
                    }

                }
            }
        });
    }

    public class ClickProxy {

        public void openPdf() {
            Intent intent = new Intent(MaintenanceDetailsActivity.this, WebViewRTCActivity.class);
            intent.putExtra("url", "file:///android_asset/pdf.html?" + APIs.BASE_URL_IMG + mStates.repairBean.get().getBuRepairInfo().getManagementCard());
            intent.putExtra("title", mStates.repairBean.get().getBuRepairInfo().getManagementCardSrc());
            startActivity(intent);
        }
    }

    public static class MaintenanceDetailsStates extends StateHolder {

        public final State<RepairBean.ListBean> dataBean = new State<>(new RepairBean.ListBean());
        public final State<Integer> type = new State<>(0);
        public final State<RepairBean> repairBean = new State<>(new RepairBean());
    }

}
